<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="100">
          <el-form-item label="商品名称：">
            <el-input v-model="listQuery.productName" class="input-width" placeholder="商品名称"></el-input>
          </el-form-item>
          <el-form-item label="推荐状态：">
            <el-select v-model="listQuery.recommendStatus" placeholder="全部" clearable class="input-width">
              <el-option v-for="item in RECOMMEND_STATUS_MAP" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearchList" size="small"> 搜索 </el-button>
            <el-button @click="handleResetSearch" size="small"> 重置 </el-button>
            <el-button size="small" class="btn-add" @click="handleSelectProduct">选择商品</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <div class="table-container">
      <el-table
        ref="newProductTable"
        height="100%"
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column type="selection" width="60" align="center"></el-table-column>
        <el-table-column label="编号" width="80" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="商品名称" align="center" show-overflow-tooltip min-width="200px">
          <template #default="scope">{{ scope.row.productName }}</template>
        </el-table-column>
        <el-table-column label="是否推荐" width="100" align="center">
          <template #default="scope">
            <el-switch
              @change="handleRecommendStatusStatusChange(scope.row)"
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.recommendStatus"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" width="80" align="center">
          <template #default="scope">{{ scope.row.sort }}</template>
        </el-table-column>
        <el-table-column label="状态" width="80" align="center">
          <template #default="scope">{{ RECOMMEND_STATUS_DICT[scope.row.recommendStatus] }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" link @click="handleEditSort(scope.row)"> 排序 </el-button>
            <el-button type="danger" size="small" link @click="handleDelete(scope.row)"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="batch-operate-container">
      <el-select size="small" v-model="operateType" placeholder="批量操作">
        <el-option v-for="item in OPERATE_STATUS_MAP" :key="item.value" :label="item.label" :value="item.value"> </el-option>
      </el-select>
      <el-button class="search-button" @click="handleBatchOperate" type="primary" size="small"> 确定 </el-button>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5, 10, 15]"
        v-model:current-page="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog title="排序" v-model="sortDialogVisible" width="40%">
      <el-form :model="sortDialogData" label-width="100">
        <el-form-item label="排序：">
          <el-input v-model="sortDialogData.sort" style="width: 200px"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="sortDialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="handleUpdateSort" size="small">确 定</el-button>
      </template>
    </el-dialog>
  </div>
  <SelectProduct ref="selectProductRef" @select="handleSelectproduct" />
</template>
<script setup lang="ts">
import { fetchList, updateRecommendStatus, deleteNewProduct, createNewProduct, updateNewProductSort } from '@/api/newProduct'
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { Hot } from '@/models/hot'
import { OPERATE_STATUS_MAP, RECOMMEND_STATUS_MAP, RECOMMEND_STATUS_DICT } from '@/constant'
import SelectProduct from '@/views/pms/product/components/SelectProduct.vue'
import type { NewProduct, NewProductPageParams, NewProductParams, NewProductSortParams } from '@/models/newProduct'

defineOptions({ name: 'NewProductList' })
const selectProductRef = ref<InstanceType<typeof SelectProduct>>()

const listQuery = ref<NewProductPageParams>({ pageNum: 1, pageSize: 10 })
const list = ref<NewProduct[]>([])
const total = ref(0)
const listLoading = ref(false)
const multipleSelection = ref<NewProduct[]>([])
const operateType = ref(null)

const sortDialogVisible = ref(false)
const sortDialogData = ref<NewProductSortParams>({ id: '' })
getList()
function handleResetSearch() {
  listQuery.value = { pageNum: 1, pageSize: 10 }
}
function handleSearchList() {
  listQuery.value.pageNum = 1
  getList()
}
function handleSelectionChange(val: NewProduct[]) {
  multipleSelection.value = val
}
function handleSizeChange(val: number) {
  listQuery.value.pageNum = 1
  listQuery.value.pageSize = val
  getList()
}
function handleCurrentChange(val: number) {
  listQuery.value.pageNum = val
  getList()
}
function handleRecommendStatusStatusChange(row: Hot) {
  updateRecommendStatusStatus(row.id + '', row.recommendStatus as number)
}
function handleDelete(row: Hot) {
  deleteProduct(row.id + '')
}

async function handleSelectproduct(row: NewProductParams) {
  await createNewProduct(row)
  getList()
  ElMessage.success('添加成功!')
}
function handleBatchOperate() {
  if (multipleSelection.value.length < 1) {
    ElMessage.warning('请选择一条记录')
    return
  }
  let ids = multipleSelection.value.map((item) => item?.id).join(',')
  if (operateType.value === 0) {
    //设为推荐
    updateRecommendStatusStatus(ids, 1)
  } else if (operateType.value === 1) {
    //取消推荐
    updateRecommendStatusStatus(ids, 0)
  } else if (operateType.value === 2) {
    //删除
    deleteProduct(ids)
  } else {
    ElMessage.warning('请选择批量操作类型')
  }
}
function handleSelectProduct() {
  selectProductRef.value?.open()
}

function handleEditSort(row: Hot) {
  sortDialogVisible.value = true
  sortDialogData.value.sort = row.sort
  sortDialogData.value.id = row.id + ''
}
async function handleUpdateSort() {
  await updateNewProductSort(sortDialogData.value)
  sortDialogVisible.value = false
  getList()
  ElMessage.success('删除成功！')
}
async function getList() {
  listLoading.value = true
  const response = await fetchList(listQuery.value)
  listLoading.value = false
  list.value = response.data.data.list
  total.value = response.data.data.total
}
async function updateRecommendStatusStatus(ids: string, status: number) {
  let params = { ids, recommendStatus: status }
  await updateRecommendStatus(params)
  getList()
  ElMessage.success('修改成功')
}
async function deleteProduct(ids: string) {
  await ElMessageBox.confirm('是否要删除该推荐?', '提示', { type: 'warning' })
  await deleteNewProduct({ ids })
  getList()
  ElMessage.success('删除成功！')
}
</script>
<style></style>
